package com.lovo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.model.Master;
import com.lovo.model.Pet;
import com.lovo.service.MasterService;
import com.lovo.service.PetService;
import com.lovo.service.impl.MasterServiceImpl;
import com.lovo.service.impl.PetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "PetServlet",urlPatterns = "/pet")
public class PetServlet extends HttpServlet {
    PetService ps=new PetServiceImpl();
    MasterService ms=new MasterServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html,charset=utf-8");
        String method=request.getParameter("method");
        if(method ==null || method.equalsIgnoreCase("")){
            selectPet(request,response);
        }
        else if(method.equalsIgnoreCase("add")){
            addPet(request,response);
        }
        else if(method.equalsIgnoreCase("delete")){
            deletePet(request,response);
        }
        else if(method.equalsIgnoreCase("update")){
            updatePet(request,response);
        }
    }
    protected void selectPet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String value=request.getParameter("value");
        String text=request.getParameter("text");
        Integer id=null;
        String masterNickname=null,masterName=null,masterTel=null;
        Master master=new Master();
        if(value.equalsIgnoreCase("")||value==null||text.equalsIgnoreCase("")||text==null){
             id=Integer.parseInt(request.getParameter("masterId"));
        }else{
            if(value.equals("1")){
                masterNickname=request.getParameter("text");
            }else if(value.equals("2")){
                masterName=request.getParameter("text");
            } else if(value.equals("3")){
                masterTel=request.getParameter("text");
            }
            master.setMasterNickname(masterNickname);
            master.setMasterName(masterName);
            master.setMasterTel(masterTel);
            id=ms.getMasterId(master);
        }




        List<Pet> list=ps.selectByPrimaryKey(id);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(list);
        System.out.println("json = " + json);

        response.getWriter().write(json);
    }
    protected void addPet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("petName");
        String race=request.getParameter("petRace");
        String type=request.getParameter("petType");
        String color=request.getParameter("petColor");
        String birthday=request.getParameter("petBirthday");
        String character=request.getParameter("petCharacter");
        Integer mid=Integer.parseInt(request.getParameter("masterId"));
//        Date date=null;
//        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        try {
//            //使用SimpleDateFormat的parse()方法生成Date
//            date = sf.parse(birthday);
//            //打印Date
//            System.out.println(date);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        Pet pet =new Pet();
        pet.setPetName(name);
        pet.setPetRace(race);
        pet.setPetType(type);
        pet.setPetColor(color);
        pet.setPetBirthday(birthday);
        pet.setPetCharacter(character);
        Master master=new Master();
        master.setMasterId(mid);
        pet.setMaster(master);

        boolean flag=ps.insert(pet);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(flag);
        System.out.println("json = " + json);

        response.getWriter().write(json);

}
    protected void deletePet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Integer id=Integer.parseInt(request.getParameter("petId"));
    boolean flag=ps.deleteByPrimaryKey(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(flag);
        System.out.println("json = " + json);

        response.getWriter().write(json);
    }
    protected void updatePet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer id=Integer.parseInt(request.getParameter("petId"));
        String name=request.getParameter("petName");
        String race=request.getParameter("petRace");
        String type=request.getParameter("petType");
        String color=request.getParameter("petColor");
        String birthday=request.getParameter("petBirthday");
        String character=request.getParameter("petCharacter");
//        Date date=null;
//        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//        try {
//            //使用SimpleDateFormat的parse()方法生成Date
//            date = sf.parse(birthday);
//            //打印Date
//            System.out.println(date);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }

        Pet pet =new Pet();
        pet.setPetId(id);
        pet.setPetName(name);
        pet.setPetRace(race);
        pet.setPetType(type);
        pet.setPetColor(color);
        pet.setPetBirthday(birthday);
        pet.setPetCharacter(character);
        Master master=new Master();
        pet.setMaster(master);

        boolean flag=ps.updatePet(pet);

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(flag);
        System.out.println("json = " + json);

        response.getWriter().write(json);

    }
}
